package com.example.lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author readpage
 * @create 2022-10-22 11:26
 * java内置的4大核心函数接口
 *
 * 消费型接口 Consumer<T>        void accept(T t)
 * 供给型接口 Supplier<T>        T get()
 * 函数型即可 Function<T, R>     R apply(T t)
 * 断定型接口 Predicate<T>       boolean test(T t)
 */
public class LambdaTest3 {
    @Test
    public void test() {
        happyTime(500, aDouble -> {
            System.out.println("学习太累了，去天上人间买了瓶矿泉水，价格为:" + aDouble);
        });
        System.out.println("************");
        happyTime(400, aDouble -> {
            System.out.println("学习太累了，去天上人间喝了水，价格为:" + aDouble);
        });
    }

    public void happyTime(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    @Test
    public void test2() {
        List<String> list = Arrays.asList("北京", "南京", "天津", "东京");
        List<String> list2 = filterString(list, s -> s.contains("京"));
        System.out.println(list2);

    }

    //根据给定的规则，过滤集合中的字符。此规则由Predicate的方法决定的
    public List<String> filterString(List<String> list, Predicate<String> predicate) {
        ArrayList<String> filterList = new ArrayList<>();
        for (String s : list) {
            if (predicate.test(s)) {
                filterList.add(s);
            }
        }
        return filterList;
    }
}
